import 'package:duqing/components/avator_role_name.dart';
import 'package:duqing/components/comment_like_read.dart';
import 'package:duqing/config/app_colors.dart';
import 'package:duqing/models/article_model.dart';
import 'package:duqing/utils/util.dart';
import 'package:flutter/material.dart';

class ArticleCard extends StatefulWidget {
  final ArticleItem articleItem;

  const ArticleCard({Key key, this.articleItem}) : super(key: key);

  @override
  _ArticleCardState createState() => _ArticleCardState();
}

const double paddingSize = 20;
const double specSize = 10;

class _ArticleCardState extends State<ArticleCard> {
  double boxSize;

  @override
  Widget build(BuildContext context) {
    boxSize = MediaQuery.of(context).size.width - paddingSize * 2;
    return Container(
      padding: EdgeInsets.all(20),
      color: Colors.white,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _title(),
          SizedBox(
            height: 20,
          ),
          // _cover(),
          _getCoverByType(),
          SizedBox(
            height: 20,
          ),
          _bottom(),
        ],
      ),
    );
  }

  Widget _title() {
    return Text(
      widget.articleItem.title,
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      style: TextStyle(
        fontSize: 16,
        color: AppColors.active,
      ),
    );
  }

  Widget _cover({int index = 0}) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(10),
      child: FadeInImage.assetNetwork(
        placeholder: 'assets/images/common/lazy-1.png',
        image: widget.articleItem.coverUrlList[index],
        fit: BoxFit.cover,
      ),
    );
  }

  Widget _cover1(){
    return SizedBox(
      width: boxSize,
      height: boxSize,
      child: _cover(),
    );
  }

  Widget _cover2() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        // Expanded(child: _cover(index: 0)),
        // Expanded(child: _cover(index: 1)),
        SizedBox(
          width: boxSize / 2 - specSize / 2,
          height: boxSize,
          child: _cover(index: 0),
        ),

        SizedBox(
          width: boxSize / 2 - specSize / 2,
          height: boxSize,
          child: _cover(index: 1),
        ),
      ],
    );
  }

  Widget _cover3() {
    return Column(
      children: [
        // Expanded(child: _cover(index: 0)),
        // Expanded(child: _cover(index: 1)),
        SizedBox(
          height: boxSize / 2 - specSize / 2,
          width: boxSize,
          child: _cover(index: 0),
        ),
        SizedBox(
          height: specSize,
        ),
        SizedBox(
          height: boxSize / 2 - specSize / 2,
          width: boxSize,
          child: _cover(index: 1),
        ),
      ],
    );
  }

  Widget _cover4() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        // Expanded(child: _cover(index: 0)),
        // Expanded(child: _cover(index: 1)),
        SizedBox(
          width: boxSize / 2 - specSize / 2,
          height: boxSize,
          child: _cover(index: 0),
        ),

        _coverC(topIndex: 1, bottomIndex: 2),
      ],
    );
  }

  Widget _cover5() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        _coverC(topIndex: 1, bottomIndex: 2),
        SizedBox(
          width: boxSize / 2 - specSize / 2,
          height: boxSize,
          child: _cover(index: 0),
        ),
      ],
    );
  }

  Widget _cover6() {
    return Column(
      children: [
        SizedBox(
          height: boxSize / 3 - specSize / 3,
          width: boxSize,
          child: _cover(index: 0),
        ),
        const SizedBox(
          height: specSize / 3,
        ),
        SizedBox(
          height: boxSize / 3 - specSize / 3,
          width: boxSize,
          child: _cover(index: 1),
        ),
        const SizedBox(
          height: specSize / 3,
        ),
        SizedBox(
          height: boxSize / 3 - specSize / 3,
          width: boxSize,
          child: _cover(index: 2),
        ),
      ],
    );
  }

  Widget _cover7() {
    return Column(
      children: [
        _coverR(leftIndex: 0, rightIndex: 1),
        const SizedBox(
          height: specSize,
        ),
        SizedBox(
          height: boxSize / 2 - specSize / 2,
          width: boxSize,
          child: _cover(index: 2),
        ),
      ],
    );
  }

  Widget _cover8() {
    return Column(
      children: [
        SizedBox(
          height: boxSize / 2 - specSize / 2,
          width: boxSize,
          child: _cover(index: 0),
        ),
        const SizedBox(
          height: specSize,
        ),
        _coverR(leftIndex: 1, rightIndex: 2),
      ],
    );
  }

  Widget _cover9() {
    return Column(
      children: [
        _coverR(leftIndex: 0, rightIndex: 1),
        const SizedBox(
          height: specSize,
        ),
        _coverR(leftIndex: 2, rightIndex: 3),
      ],
    );
  }

  Widget _coverR({int leftIndex, int rightIndex}) {
    return Row(
      children: [
        SizedBox(
          height: boxSize / 2 - specSize / 2,
          width: boxSize / 2 - specSize / 2,
          child: _cover(index: leftIndex),
        ),
        const SizedBox(
          width: specSize,
        ),
        SizedBox(
          height: boxSize / 2 - specSize / 2,
          width: boxSize / 2 - specSize / 2,
          child: _cover(index: rightIndex),
        ),
      ],
    );
  }

  Widget _coverC({int topIndex, int bottomIndex}) {
    return Column(
      children: [
        SizedBox(
          height: boxSize / 2 - specSize / 2,
          width: boxSize / 2 - specSize / 2,
          child: _cover(index: topIndex),
        ),
        SizedBox(
          height: specSize,
        ),
        SizedBox(
          height: boxSize / 2 - specSize / 2,
          width: boxSize / 2 - specSize / 2,
          child: _cover(index: bottomIndex),
        ),
      ],
    );
  }

  Widget _bottom() {
    return Row(
      children: [
        Expanded(
          child: AvatarRoleName(
            nickName: widget.articleItem.user.nickname,
            avatar: widget.articleItem.user.coverPictureUrl,
            type: widget.articleItem.user.type,
          ),
        ),
        Expanded(
          child: CommentLikeRead(
            commentCount: widget.articleItem.commentCount,
            readCount: widget.articleItem.readCount,
            thumbUpCount: widget.articleItem.thumbUpCount,
          ),
        ),
      ],
    );
  }

  int getTypeByLength() {
    int length = widget.articleItem.coverUrlList.length;
    int type;
    switch (length) {
      case 2:
        {
          type = getRandomRangeInt(2, 3);
          break;
        }

      case 3:
        {
          type = getRandomRangeInt(2, 8);
          break;
        }

      case 4:
        {
          type = getRandomRangeInt(2, 9);
          break;
        }
      default:
        {
          type = 1;
        }
    }
    return type;
  }

  Widget _getCoverByType() {
    int coverType = getTypeByLength();
    // int coverType = 9;
    Widget coverWidget;

    switch (coverType) {
      case 2:
        {
          coverWidget = _cover2();
          break;
        }
      case 3:
        {
          coverWidget = _cover3();
          break;
        }
      case 4:
        {
          coverWidget = _cover4();
          break;
        }
      case 5:
        {
          coverWidget = _cover5();
          break;
        }
      case 6:
        {
          coverWidget = _cover6();
          break;
        }
      case 7:
        {
          coverWidget = _cover7();
          break;
        }
      case 8:
        {
          coverWidget = _cover8();
          break;
        }
      case 9:
        {
          coverWidget = _cover9();
          break;
        }
      default:
        {
          coverWidget = _cover1();
          break;
        }
    }
    return coverWidget;
  }

  Widget _oneImage() {
    return ClipRRect(
      borderRadius: BorderRadius.circular(10),
      child: FadeInImage.assetNetwork(
        placeholder: 'assets/images/common/lazy-1.png',
        image: widget.articleItem.coverUrlList[0],
        fit: BoxFit.cover,
      ),
    );
  }

  Widget _towImages() {
    return ClipRRect(
      borderRadius: BorderRadius.circular(10),
      child: FadeInImage.assetNetwork(
        placeholder: 'assets/images/common/lazy-1.png',
        image: widget.articleItem.coverUrlList[0],
        fit: BoxFit.cover,
      ),
    );
  }
}
